home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12584 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  50 lines

  1. Newsgroups: comp.lang.c++
  2. From: jgustafs@sisna.com (Josh Gustafson)
  3. Subject: A question on generic classes
  4. Organization: Source Internet Services
  5. X-Newsreader: News Xpress 2.0 Beta #0
  6. Date: Tue, 19 Mar 96 03:55:12 GMT
  7. NNTP-Posting-Host: dialup1147.sisna.com
  8. Message-ID: <314e1c88.0@news.sisna.com>
  9. Path: news.sisna.com!DIALUP1147
  10.  
  11. Hi all!
  12.  
  13. I'm giving myself a crash course in C++, and I have a question regarding 
  14. generic classes.
  15.  
  16. Say I've got some silly little template:
  17.  
  18. template <class the_type> class blah {
  19.   the_type data;
  20. public:
  21.   blah( the_type param ) { data = param };
  22. }
  23.  
  24. Ok, now if I want to declare an object that uses, say, ints, i do:
  25.  
  26. blah<int> my_var( some_int_value );
  27. blah<int> my_other_var = blah( some_int_value );
  28.  
  29. Now, say I want to declare a specialized version of this template that always 
  30. uses ints as the_type.  Could I do this with a typedef?
  31.  
  32. typedef blah<int> intblah;
  33. intblah my_var( some_int_value );
  34. intblah my_other_var = intblah( some_int_value );
  35.  
  36. And, if so, what will most compilers (Watcom C++ 10.5, in particular) about 
  37. optimizing this?  That is, will the template simply be expanded in place of 
  38. each reference to intblah, or would it create a new class with ints plugged in 
  39. place of the_type?
  40.  
  41. If the above does not work, what is the best way to do what I'm trying to do?
  42.  
  43. If this question hasn't made any sense, let me know and I'll see if i can't 
  44. clear it up a bit...
  45.  
  46.  
  47.  
  48. --
  49. Josh Gustafson  (jgustafs@sisna.com)
  50.